<?php

namespace app\command;

use Illuminate\Support\Facades\DB;
use support\Redis;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\Output\BashCompletionOutput;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use yzh52521\EasyHttp\Http;


class WxRun extends Command
{
    protected static $defaultName = 'wx:run';
    protected static $defaultDescription = 'wx-run';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
    }

    /**
     * 获取access_token
     * @return string
     */
    protected function getAccessToken(){
        $key = 'rs:wx:access_token';
        $access_token = Redis::get($key);
        if(empty($access_token)){
            $config_data = config('wx');
            $req_data_arr = Http::get($config_data['bash_url'].'access_token',['appid'=>$config_data['appid'],'app_secret'=>$config_data['app_secret']]);

            $req_data_arr = $req_data_arr->array();

            if(!empty($req_data_arr['data']['access_token'])){
                $access_token = $req_data_arr['data']['access_token'];
                Redis::set($key,$access_token,'EX',6000);
            }
        }
        return $access_token;
    }
    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {

        $name = $input->getArgument('name');

        $output->writeln('start...');

        $access_token = $this->getAccessToken();

        //根据客户extID获取客户详情获得微盛userid
        $bash_url = config('wx.bash_url');

        $file_root = public_path();

        $read_file = $file_root.'/0920.json';

        $json = file_get_contents($read_file);

        $file_arr = json_decode($json,1);

        //进度条
        $progress = new ProgressBar($output,count($file_arr));

        $progress->start();

        foreach ($file_arr as $value){

            $user_info_arr = Http::get($bash_url.'customer/info/detail',['external_userid'=>$value['wsid'],'access_token'=>$access_token])->array();

            $user_id = $user_info_arr['data']['follow_user'][0]['userid'];

            $external_userid = $user_info_arr['data']['external_userid'];

            //设定指定标签
            $req_arr = ['user_id'=>$user_id,'external_userid'=>$external_userid,'field_map'=>['mf_customer_51'=>$value['user_id']]];

            $res = Http::asJson()->post($bash_url.'customer/edit?access_token='.$access_token,$req_arr)->array();

            if($res['code']){
                $output->writeln('user_id:'.$value['user_id'].',error:'.$res['msg']);
            }

            $progress->advance();

            print_r($res);
            return self::SUCCESS;
        }

        $progress->finish();

        return self::SUCCESS;
    }

}
